package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.LEmployee;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LGoodsMapper;
import com.ruoyi.system.domain.LGoods;
import com.ruoyi.system.service.ILGoodsService;

import javax.validation.Validator;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 货品资料Service业务层处理
 *
 * @author zyb
 * @date 2022-01-02
 */
@Service
public class LGoodsServiceImpl extends ServiceImpl<LGoodsMapper, LGoods> implements ILGoodsService {


    @Autowired
    private ISysConfigService configService;
    @Autowired
    protected Validator validator;

    /**
     * 查询货品资料
     *
     * @param goodsid 货品资料主键
     * @return 货品资料
     */
    @Override
    public LGoods selectLGoodsByGoodsid(Long goodsid) {
        return baseMapper.selectById(goodsid);
    }

    /**
     * 查询货品资料列表
     *
     * @param lGoods 货品资料
     * @return 货品资料
     */
    @Override
    public List<LGoods> selectLGoodsList(LGoods lGoods) {
        return baseMapper.selectLGoodsList(lGoods);
    }

    /**
     * 新增货品资料
     *
     * @param lGoods 货品资料
     * @return 结果
     */
    @Override
    public int insertLGoods(LGoods lGoods) {
        lGoods.setCreateBy(String.valueOf(getUserId()));
        lGoods.setCreateTime(DateUtils.getNowDate());

        return baseMapper.insert(lGoods);
    }

    /**
     * 修改货品资料
     *
     * @param lGoods 货品资料
     * @return 结果
     */
    @Override
    public int updateLGoods(LGoods lGoods) {
        lGoods.setUpdateBy(String.valueOf(getUserId()));
        lGoods.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(lGoods);
    }

    /**
     * 批量删除货品资料
     *
     * @param goodsids 需要删除的货品资料主键
     * @return 结果
     */
    @Override
    public int deleteLGoodsByGoodsids(Long[] goodsids) {
        return baseMapper.deleteLGoodsByGoodsids(goodsids);
    }

    /**
     * 删除货品资料信息
     *
     * @param goodsid 货品资料主键
     * @return 结果
     */
    @Override
    public int deleteLGoodsByGoodsid(Long goodsid) {
        return baseMapper.deleteById(goodsid);
    }

    @Override
    public String importGoods(List<LGoods> goodsList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(goodsList) || goodsList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LGoods goods : goodsList)
        {
            try
            {
                // 验证是否存在这个用户
                LGoods g=this.baseMapper.selectOne(new LambdaQueryWrapper<LGoods>().eq(LGoods::getCode,goods.getCode()));
                 if (StringUtils.isNull(g))
                {
                    BeanValidators.validateWithException(validator, goods);
                    this.insertLGoods(goods);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、货品 " + goods.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, goods);
                    goods.setGoodsid(g.getGoodsid());
                    this.updateLGoods(goods);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、货品 " + goods.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、货品 " + goods.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、货品 " + goods.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public Long getId( LGoods goods) {
        List<LGoods> lGoodsList= this.baseMapper.selectList(new LambdaQueryWrapper<LGoods>(goods));
        if(lGoodsList.size()>0)return lGoodsList.get(0).getGoodsid();
        return 0L;
    }
}
